const app = getApp()

Page({
    data: {
        address: {},
        prompt: {
            hidden: !0,
            icon: '../../images/iconfont-addr-empty.png',
            title: '还没有收货地址呢',
            text: '暂时没有相关数据',
        },
    },
  onLoad: function (options) {
      /*var that = this
      //页面初始化，option为页面跳转所带来的参数
      var openid = options.openid
      console.log("openid",openid)
      wx.request({
        url: 'http://localhost:8080/demo/userinfo/getuserinfobyopenid',
        method: 'GET',
        data: { "buyerOpenid": openid },//传入的参数
        success: function (res) {
          var list = res.data.userInfo;
          if (list == null) {
            var toastText = '获取数据失败' + res.data.errMsg;
            wx.showToast({
              title: toastText,
              icon: '',
              duration: 2000
            });
          } else {
            that.setData({
              userInfoItems: list
            });
          }
        }
      })*/

        /*this.address = App.HttpResource('/address/:id', {id: '@id'})*/
    },
  onShow: function(options) {

      var that = this
      //页面初始化，option为页面跳转所带来的参数
      let openid = wx.getStorageSync("openid");
      //var openid = options.openid
      console.log("openid", openid)
      wx.request({
        url: app.globalData.baseUrl +'/userinfo/getuserinfobyopenid',
        method: 'GET',
        data: { "buyerOpenid": openid },//传入的参数
        success: function (res) {
          var list = res.data.userAddressinfo;
          if (list == null) {
            var toastText = '获取数据失败' + res.data.errMsg;
            wx.showToast({
              title: toastText,
              icon: '',
              duration: 2000
            });
          } else {
            that.setData({
              userAddressinfo: list
            });
          }
        }
      })
        //this.onPullDownRefresh()
    },
    initData() {
        this.setData({
            address: {
                items: [],
                params: {
                    page : 1,
                    limit: 10,
                },
                paginate: {}
            }
        })
    },
    
    setDefalutAddress(e) {
        const id = e.currentTarget.dataset.id
       /* App.HttpService.setDefalutAddress(id)
        .then(data => {
            console.log(data)
            if (data.meta.code == 0) {
                this.onPullDownRefresh()
            }
        })*/
    },
  addAddress: function () {
    wx.navigateTo({
      url: '../../address/edit/index',
    })
  },
    
    
  deleteAddress:function(e){
    var that= this;
    console.log("addressId:" +e.currentTarget.dataset.addressid)
    wx.showModal({
      title: '提示',
      content: '确定要删除该地址吗？',
      
      success:function(sm){
       if (sm.confirm) {
          wx.request({
            url: app.globalData.baseUrl +'/userinfo/removeuseraddress',
            data: { "addressId": e.currentTarget.dataset.addressid},

            method:'GET',
            success:function(res){
              var result= res.data.success;
              var toastText="删除成功！";
              if(result!=true){
                toastText="删除失败";
              }else{
                that.data.userAddressinfo.splice(e.target.dataset.index,1)
                that.setData({
                  userAddressinfo: that.data.userAddressinfo
                });
              }
              wx.showToast({
                title: toastText,
                icon: '',
                duration: 2000
              });
            }
         })
       }
      }
    })
  }
})